﻿using Microsoft.DirectX.Direct3D;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleLiteForm
{
    /// <summary>
    /// The drawing manager to higlight text.
    /// </summary>
    public class DrawingManager : IDisposable
    {
        /// <summary>
        /// The marker color.
        /// </summary>
        public readonly Color COLOR = Color.FromArgb(10, Color.Yellow);

        /// <summary>
        /// The sprite.
        /// </summary>
        private Sprite _sprite;

        /// <summary>
        /// The texture.
        /// </summary>
        private Texture _texture;

        /// <summary>
        /// The control to render on.
        /// </summary>
        private Control _control;

        /// <summary>
        /// The points to draw.
        /// </summary>
        private IList<PointF> _pointList = new List<PointF>();

        /// <summary>
        /// Indicates wheter the user is currently drawing.
        /// </summary>
        private bool _isDrawing = false;

        /// <summary>
        /// Indicates whether the edit state is enabled.
        /// </summary>
        private bool _isEditingEnabled;

        /// <summary>
        /// Creates a DrawingManager instance.
        /// </summary>
        /// <param name="device">The graphics device.</param>
        /// <param name="control">The control panel to draw on an listen to mouse events.</param>
        public DrawingManager(Device device, Control control)
        {
            _control = control;

            _control.MouseDown += (s, e) =>
                {
                    if (!IsEditingEnabled)
                        return;

                    _isDrawing = true;
                };

            _control.MouseMove += (s, e) =>
            {
                if (!IsEditingEnabled || !_isDrawing)
                    return;

                var pos = new PointF(e.Location.X - 10, e.Location.Y - 10);

                if (!_pointList.Contains(pos))
                    _pointList.Add(pos);
            };

            _control.MouseUp += (s, e) =>
            {
                _isDrawing = false;
            };

            _control.MouseLeave += (s, e) =>
            {
                _isDrawing = false;
            };

            _sprite = new Sprite(device);
            _texture = TextureLoader.FromFile(device, "../../assets/textures/blob.png");
            _isEditingEnabled = false;
        }

        /// <summary>
        /// Draws the user highlights.
        /// </summary>
        public void Draw()
        {
            _sprite.Begin(SpriteFlags.AlphaBlend | SpriteFlags.SortTexture);
            
            foreach (var point in _pointList)
            {
                _sprite.Draw2D(_texture, Rectangle.Empty, Size.Empty, point, COLOR);
            }

            _sprite.End();
        }

        /// <summary>
        /// Cleas the user drawings.
        /// </summary>
        public void Clear()
        {
            _pointList.Clear();
        }

        /// <summary>
        /// Cleans up the unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_texture != null)
                _texture.Dispose();
            if (_sprite != null)
                _sprite.Dispose();
        }

        /// <summary>
        /// Gets or sets the edit state.
        /// </summary>
        public bool IsEditingEnabled
        {
            get
            {
                return _isEditingEnabled;
            }
            set
            {
                _isEditingEnabled = value;

                if (_isEditingEnabled)
                    _control.Cursor = Cursors.Cross;
                else
                    _control.Cursor = Cursors.Default;
                
            }
        }
    }
}
